package com.whalehearing.tingyou.common.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
    private Set<Integer>set=new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] value = constraintAnnotation.vals();
        if(value.length>0){
            for (int i = 0; i < value.length; i++) {
                set.add(value[i]);
            }
        }
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    /* *
     * 包含返回true 验证通过，反之返回false
     * @param value 需要核验的值
     * @param context
     * @return boolean
     * @author shensh
     * @create 2024/5/26
     **/
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
